package logic

import (
	"context"

	"go-papers/models"
	"go-papers/pkg/lognew"
)

func UpdateOrganMomNums(momId int) {

	if momId <= 0 {
		return
	}
	var organ models.Organ
	count := organ.GetCountByMom(momId)

	var momIn models.MemberMom
	momInfo, _ := momIn.GetByMID(momId)
	lognew.Info(context.TODO(), "UpdateOrganMomNums", lognew.Fields{"momInfo": momInfo, "count": count})
	if momInfo.MomOrgan != count {
		momInfo.MomOrgan = count
		_, _ = momInfo.Update(momInfo.MemberMomId)
	}
}

func UpdateMomNewUser(momId int) int {
	if momId <= 0 {
		return 0
	}
	var member models.Member
	count, count2 := member.GetCountByMom(momId) // MomCustomer 客户输了 /  达人数量

	var momIn models.MemberMom
	momInfo, _ := momIn.GetByMID(momId)
	if momInfo.MomNewer != count {

		lognew.Info(context.TODO(), "", lognew.Fields{"momInfo": momInfo, "count": count, "count2": count2})

		momInfo.MomCustomer = count
		momInfo.MomNewer = count2

		_, _ = momInfo.Update(momInfo.MemberMomId)
	}

	return count
}

func GetLevel(count int) int {
	// 获取等级配置
	var conf models.CommonCfg
	finfo := conf.GetCfg()

	levelCount := finfo.MemberNums
	if levelCount > 0 {
		ll := count / levelCount
		if ll <= 0 {
			ll = 0
		}
		return ll
	} else {
		return 0
	}
}
